import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import * as _ from 'lodash';
import { CacheManager } from '@midwayjs/cache';
import { Context } from '@midwayjs/koa';
import { BusinessProductABatchEntity } from '../../entity/product/a_batch';

/**
 * business模块-产品-A-批次
 */
@Provide()
export class BusinessProductABatchService extends BaseService {
  @InjectEntityModel(BusinessProductABatchEntity)
  businessProductABatchEntity: Repository<BusinessProductABatchEntity>;

  @Inject()
  cacheManager: CacheManager;

  @Inject()
  ctx: Context;

  /**
   * 获取分页数据（用户端）
   */
  async getPageByUser(query: any) {
    const { userId } = this.ctx.admin;
    const option = {
      fieldEq: ['batchNo'],
      where: async (ctx: Context) => {
        const res = []
        if (query.date) {
          const date_query = ["createTime > :date_start AND createTime < :date_end", { date_start: `${query.date} 00:00:00`, date_end: `${query.date} 23:59:59` }]
          res.push(date_query)
        }
        return res
      }
    }
    query.userId = userId;
    return await super.page(query, option);
  }
}
